package jd;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileWriter;
import java.io.IOException;

public class demo1 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException {
        //工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        //解析XML
        Document doc = db.parse("src/jd/demo1.xml");
        //得到所有Student节点列表信息
        NodeList studentList = doc.getElementsByTagName("Student");
        for (int i = 0; i < studentList.getLength(); i++) {
            Element student= (Element) studentList.item(i);
            NodeList element=student.getChildNodes();
            String id=student.getAttribute("id");
            System.out.println("学号:"+id);
            for (int j = 0; j <element.getLength() ; j++) {
//                if (element.item(j).getNodeType()== Node.ELEMENT_NODE){
//                    System.out.println(element.item(i).getTextContent());
//                }
                System.out.println(element.item(i).getTextContent());
            }
        }

        NodeList list=doc.getElementsByTagName("Student");

        for (int i = 0; i <list.getLength(); i++) {
            Element element= (Element) list.item(i);
            String id=element.getAttribute("id");
            NodeList list1=element.getChildNodes();
            if (id.equals("1")){
                for (int j = 0; j < list1.getLength(); j++) {
                    Element element1= (Element) list1.item(j);
                    String a=element1.getNodeName();
                    if (a.equals("score")){
                        element.getParentNode().removeChild(element1);
                    }
                }
            }
            if (id.equals("2")){
                for (int j = 0; j < list1.getLength(); j++) {
                    Element element1= (Element) list1.item(j);
                    String a=element1.getNodeName();
                    if (a.equals("score")){
                        element1.setTextContent("60");
                    }
                }
            }
        }

        Element student=doc.createElement("Student");
        student.setAttribute("id","3");
        Element name=doc.createElement("name");
        student.setTextContent("xlj");
        Element course=doc.createElement("course");
        student.setTextContent("xlj");
        Element score=doc.createElement("score");
        student.appendChild(name);
        student.appendChild(course);
        student.appendChild(score);

        Element phoneInfo= (Element) doc.getElementsByTagName("PhoneInfo").item(0);
        phoneInfo.appendChild(student);



        //保存XML文件
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(doc);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
        StreamResult streamResult = new StreamResult(new FileWriter("src/jd/demo1.xml"));
        //把DOM树转换为XML文件
        transformer.transform(domSource,streamResult);


    }
}
